package com.qy.qDemo.myTest

import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.qy.common.utils.Tip
import com.qy.qDemo.R
import com.qy.qDemo.myTest.model.TestModel
import com.qy.qDemo.myTest.model.TestViewModelFactory

/**
 * 当前包名：
 * 作者：zcq
 * 时间：2021/11/30 17:31
 * 描述：测试类
 */
class TestKtActivity : AppCompatActivity() {
    lateinit var textview: TextView
    lateinit var viewModel: TestModel
    private val rv: RecyclerView by lazy { findViewById(R.id.rv) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test_kt)
        textview = findViewById<TextView>(R.id.tv_text)
        viewModel = ViewModelProvider(this, TestViewModelFactory(5)).get(TestModel::class.java)
        viewModel.counter.observe(this, Observer {
            textview.text = it.toString()
        })
        //refreshCounter()
        initAdapter()
    }

    private fun initAdapter() {
        val list = mutableListOf<Int>()
        for (i in 0..20) {
            list.add(i)
        }
        val myAdapter = MyAdapter(list)
        val gridLayoutManager = GridLayoutManager(this, 4)
        rv.layoutManager = gridLayoutManager
        rv.adapter = myAdapter
        myAdapter.setOnItemClickListener { adapter, view, position ->
            when (position) {
                0 -> {
                    Tip.show("0")
                }
                1 -> {
                    viewModel.plusOne()
                }
                2 -> {
                    viewModel.clear()
                }
            }
        }
    }

    private fun refreshCounter() {
        textview.text = viewModel.counter.toString()
    }

    class MyAdapter(data: MutableList<Int>?) :
        BaseQuickAdapter<Int, BaseViewHolder>(R.layout.recycle_item_text_view, data) {
        override fun convert(holder: BaseViewHolder, item: Int) {
            holder.setText(R.id.tv, "test->$item")
        }
    }
}